package com.feather.tenia.adapter.data;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import com.feather.common.data.Column;
import com.feather.common.data.ColumnCollection;
import com.feather.tenia.MetaDataColumnHandler;

/**
 * @author flogyin
 */
public class MetaData implements java.io.Serializable {
	private static final long	serialVersionUID	= 1L;

	public int					count;
	public String				catalog;
	public String				schema;
	public String				table;

	private MetaData() {
	}

	public static MetaData load(ResultSetMetaData rsmd, ColumnCollection collection, MetaDataColumnHandler handler) throws SQLException {
		MetaData metaData = new MetaData();
		if (rsmd != null) {
			metaData.count = rsmd.getColumnCount();
			for (int i = 1; i <= metaData.count; i++) {
				String catalogName = rsmd.getCatalogName(i);
				// String columnClassName = rsmd.getColumnClassName(i);
				String columnLabel = rsmd.getColumnLabel(i);
				if (columnLabel == null || columnLabel.length() == 0) {
					columnLabel = rsmd.getColumnName(i);
				}
				String columnTypeName = rsmd.getColumnTypeName(i);
				String schemaName = rsmd.getSchemaName(i);
				String tableName = rsmd.getTableName(i);
				int columnType = rsmd.getColumnType(i);
				int columnDisplaySize = rsmd.getColumnDisplaySize(i);
				int precision = -1;
				try {
					precision = rsmd.getPrecision(i);
				} catch (NumberFormatException ex) {
				}
				int scale = rsmd.getScale(i);

				metaData.catalog = catalogName;
				metaData.schema = schemaName;
				metaData.table = tableName;

				Column column = collection.add();
				// if (source != null) {
				// column.setSchema(source.getSchema());
				// }
				column.setName(columnLabel);
				column.setDataType(columnType);
				column.setDataTypeName(columnTypeName);
				column.setOrdinal(i);
				column.setLength(columnDisplaySize);
				column.setPrecision(precision);
				column.setScale((short) scale);

				if (handler != null)
					handler.onLoad(rsmd, i, column);
			}
		}
		return metaData;
	}

	public void printMeta(ResultSet rs) throws SQLException {
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		for (int i = 1; i <= columnCount; i++) {
			System.out.println("getCatalogName[" + i + "]:" + rsmd.getCatalogName(i));
			System.out.println("getColumnClassName[" + i + "]:" + rsmd.getColumnClassName(i));
			System.out.println("getColumnLabel[" + i + "]:" + rsmd.getColumnLabel(i));
			System.out.println("getColumnName[" + i + "]:" + rsmd.getColumnName(i));
			System.out.println("getColumnTypeName[" + i + "]:" + rsmd.getColumnTypeName(i));
			System.out.println("getSchemaName[" + i + "]:" + rsmd.getSchemaName(i));
			System.out.println("getTableName[" + i + "]:" + rsmd.getTableName(i));
			System.out.println("getColumnType[" + i + "]:" + rsmd.getColumnType(i));
			System.out.println("getColumnDisplaySize[" + i + "]:" + rsmd.getColumnDisplaySize(i));
			System.out.println("getColumnTypeName[" + i + "]:" + rsmd.getColumnTypeName(i));
		}
	}
}
